package com.bookstore.Controller;

import com.bookstore.Domain.*;
import com.bookstore.Service.AddressService;
import com.bookstore.Service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 未来
 */
@CrossOrigin
@Controller
@RequestMapping("/announcements")
public class AnnouncementController {


    private final AnnouncementService announcementService;

    @Autowired
    public AnnouncementController(AnnouncementService announcementService) {
        this.announcementService = announcementService;
    }


    /**
     * 获取全部用户
     * @return ：用户集合
     */
    @GetMapping
    @ResponseBody
    public Result getAll() {
        List<Announcement>  announcements = announcementService.findAll();
        Integer code = announcements != null ? Code.GET_OK : Code.GET_ERR;
        String msg = announcements != null ? Code.MSG_OK : Code.MSG_ERR;
        return new Result(code,announcements, msg);
    }

    @PostMapping
    @ResponseBody
    public Result save(@RequestBody Announcement announcement) {
        boolean flag = announcementService.saveShopping(announcement);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public Result deleteBook(@PathVariable Integer id) {
        boolean flag = announcementService.deleteAnnouncement(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }
}
